package com.fan.configserver.controller;

import com.fan.configserver.exception.QhException;
import com.fan.configserver.exception.QhUserErrors;
import com.fan.configserver.po.Application;
import com.fan.configserver.po.ResourceItem;
import com.fan.configserver.service.ApplicationService;
import com.fan.configserver.service.ResourceItemService;
import com.fan.configserver.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * java类简单作用描述
 *
 * @Description: java类作用描述
 * @Author: hf
 * @CreateDate: 2018/9/5 15:59
 * @UpdateUser: hf
 * @UpdateDate: 2018/9/5 15:59
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
@RestController
@RequestMapping("/application")
public class ApplicationController {
    @Autowired
    ApplicationService applicationService;

    @GetMapping("/getApplication/{applicationId}")
    public ResponseResult getApplicationById(@PathVariable Long applicationId) {
        ResponseResult responseResult = new ResponseResult();
        ApplicationVo applicationVo = new ApplicationVo();
        Application application = applicationService.getApplicationById(applicationId);
        BeanUtils.copyProperties(application, applicationVo);
        responseResult.setData(applicationVo);
        return responseResult;
    }


    @DeleteMapping("/delApplication/{applicationId}")
    public ResponseResult delApplication(@PathVariable Long applicationId) {
        applicationService.delApplication(applicationId);
        return new ResponseResult();
    }


    @PutMapping("/updateApplication/{applicationId}")
    public ResponseResult updateApplication(@PathVariable Long applicationId, ApplicationRequest applicationRequest) {
        Application application = new Application();
        BeanUtils.copyProperties(applicationRequest, application);
        applicationService.updateApplication(application);
        return new ResponseResult();
    }

    @PostMapping("/addApplication/{applicationId}")
    public ResponseResult addApplication(@PathVariable Long applicationId, ApplicationRequest applicationRequest) {
        Application application = new Application();
        BeanUtils.copyProperties(applicationRequest, application);
        applicationService.addApplication(application);
        return new ResponseResult();
    }


    @GetMapping("/getAllApplicationByPage")
    public ResponseResult getAllApplicationByPage(MyPage myPage, Long userId) {
        ResponseResult responseResult = new ResponseResult();
        JsonPage<Application> allApplicationByPage = applicationService.getAllApplicationByPage(myPage, userId);
        List<ApplicationVo> result = new ArrayList<ApplicationVo>();
        for (Application application : allApplicationByPage.getRows()) {
            ApplicationVo applicationVo = new ApplicationVo();
            BeanUtils.copyProperties(application, applicationVo);
            result.add(applicationVo);
        }
//        allApplicationByPage.setRows();
        responseResult.setData(allApplicationByPage);
        return responseResult;
    }


}
